package com.laviedesfons.mavenjhmtascplugin;

import java.io.File;
import java.io.FileFilter;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;

/**
 *
 * @author Romano
 */
public abstract class ParentMojo extends AbstractMojo {

    /**
     * @parameter default-value="false"
     */
    protected Boolean verbose;
    /**
     * @parameter default-value="false"
     */
    protected Boolean debug;
    /**
     * @parameter default-value="800:600:20"
     */
    protected String header;
    /**
     * @parameter default-value="no"
     */
    protected String mainClass;
    /**
     * @parameter default-value="${project.build.sourceDirectory}"
     */
    protected String sourceDir;
    /**
     * @parameter default-value="${project.build.directory}"
     */
    protected String outputDir;
    /**
     * @parameter default-value="${project.build.finalName}"
     */
    protected String finalName;
    /**
     * @parameter default-value="sameDomain"
     */
    protected String allowScriptAccess;
    /**
     * @parameter default-value="${project.artifactId}"
     */
    protected String name;
    /**
     * @parameter default-value="${project.artifactId}"
     */
    protected String id;
    /**
     * @parameter default-value="high"
     */
    protected String quality;
    /**
     * @parameter default-value="#ffffff"
     */
    protected String bgcolor;
    /**
     * @parameter default-value="middle"
     */
    protected String align;
    /**
     * @parameter default-value="${plugin.artifacts}"
     */
    protected List<Artifact> pluginArtifacts;

    protected void log(String string) {
        if (verbose) {
            getLog().info(string);
        }
    }

    protected void debug(String string){
        if(debug) {
            getLog().debug(string);
        }
    }

    private String[] tokenizedHeader = null;
    
    // inner class
    protected class CustomFileFilter implements FileFilter {

        private String[] okFileExtensions = null;

        public CustomFileFilter(String[] fileExtensions) {
            okFileExtensions = fileExtensions;
        }

        @Override
        public boolean accept(File file) {
            if (okFileExtensions != null && okFileExtensions.length > 0) {
                for (String extension : okFileExtensions) {
                    if (file.getName().toLowerCase().endsWith(extension)) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    private void splitHeader(){
        if(tokenizedHeader == null){
            tokenizedHeader = header.split(":");
            if(tokenizedHeader.length != 3){
                getLog().error("unable to split header into 3 parts: with:height:framerate");
            }
        }
    }

    protected int getHeaderWidth() {
        splitHeader();
        return Integer.parseInt(tokenizedHeader[0]);
    }

    protected int getHeaderHeight() {
        splitHeader();
        return Integer.parseInt(tokenizedHeader[1]);
    }

    protected int getHeaderFrameRate() {
        splitHeader();
        return Integer.parseInt(tokenizedHeader[2]);
    }
}
